import routes from './routes'
import Router from 'vue-router'
import Vue from 'vue'
import store from '@/store'

Vue.use(Router)
const router = new Router({
  mode: 'history',
  routes
})
router.beforeEach(function (to, from, next) {
  if (to.meta.nologin) {
    next()
    return false
  }
  const token = sessionStorage.getItem('token')
  if (!token) {
    next('/login')
    return false
  }
  const _setToken = store.getters['common/token']
  // 如果登录页面的token与存在store中的token不一致，则表示没有登录权限，跳转到登录页面
  if (_setToken !== token) {
    next('/login')
    return false
  }
  next()
})
export default router
